/*
 * @Descripttion:
 * @Author: Cxy
 * @Date: 2022-12-29 14:18:49
 * @LastEditors: Cxy
 * @LastEditTime: 2025-10-15 23:26:10
 * @FilePath: \xy-admin\play\src\main.ts
 */
import { createApp } from 'vue'

/* rem */
import './utils/rem'
/* xy组件 */
import XyElement from '@xy-admin/elecomponents'
import '@xy-admin/elecomponents/dist/style.css'
/* i18n  */
import { i18n } from './i18n'
import type { UploadProgressEvent } from 'element-plus'
import logo from '@/views/login/component/logo.vue'
/* 全局样式 */
import '@/style/main.scss'
import { XyIconRender } from '@/components/XyIcon'
import { controller } from '@/api/axios'
import { postZhengchangUploadApi } from '@/api/example'

import pinia from '@/stores'

import App from '@/App.vue'
import router from '@/router'

const app = createApp(App)
/* 全局组件注册 使用自动引入组件 unplugin-vue-components/vite 不需要全局注册 */
// import comInstall from '@/components'
// app.use(comInstall)
/* elementPlus 使用自动引入组件 unplugin-vue-components/vite 不需要全局注册 */
// import ElementPlus from 'element-plus'
// import 'element-plus/dist/index.css'
// app.use(ElementPlus, { size: 'small', zIndex: 2000 })
app.use(pinia)
app.use(i18n)
app.use(router)
app.use(XyElement, {
  form: {
    datePicker: { defaultTime: [new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 1, 1, 23, 59, 59)] },
    upload: {
      uploadMethod: options => {
        const abortKey = Symbol('id')
        const formData = new FormData()
        formData.append('file', options.file)
        // 第一帧上传
        async function videoFirstFrameUploadMethod(options: XyUploadRequestOptions) {
          if (!options.videoFirstFrameFile) return ''
          const formatDate = new FormData()
          formatDate.append('file', options.videoFirstFrameFile)
          const {
            data: { url }
          } = await postZhengchangUploadApi(formatDate, () => {})
          return url
        }
        postZhengchangUploadApi(
          formData,
          evt => {
            const progressEvt = evt as unknown as UploadProgressEvent
            progressEvt.percent = evt.total ? (evt.loaded / evt.total) * 100 : 100
            options.onProgress(progressEvt)
          },
          { abortKey }
        )
          .then(async res => {
            const videoFirstFrameUrl = await videoFirstFrameUploadMethod(options)
            options.onSuccess({ name: options.file.name, url: res.data.url, videoFirstFrameUrl })
          })
          .catch(err => {
            options.onError(err)
          })
        options.abortRequest = () => controller.abortRequest(abortKey)
      }
    }
  },
  table: {
    pageConf: {
      currentPageAlias: 'pageNum',
      prevIcon: XyIconRender('streamline:button-previous-solid'),
      nextIcon: XyIconRender('streamline:button-next-solid')
    },
    loadingConf: {
      target: 'tablePag'
    },
    emptySlot: () =>
      h('div', { style: { display: 'flex', justifyContent: 'center', alignItems: 'center', height: '120px' } }, [
        h(logo)
      ])
  }
})
app.mount('#app')
